import { defineStore } from 'pinia'
import { ref } from 'vue'
import { getUserInfoService } from '../api/user'

//用户模块   state:   token
//          actions:  setToken  removeToken

export const useUserStore = defineStore(
  'user',
  () => {
    const token = ref('')
    function setToken(newToken: string) {
      token.value = newToken
    }
    function removeToken() {
      token.value = ''
    }

    //拿到用户数据
    const user: any = ref({})
    const getUser = async () => {
      const res = await getUserInfoService()
      //   console.log(res)
      user.value = res.data.data
    }

    const setUser = (obj: Object) => {
      user.value = obj
    }

    return { token, setToken, removeToken, user, getUser, setUser }
  },
  {
    persist: true
  }
)
